package middleware

import (
	"github.com/gin-gonic/gin"
	"mygo/app/models"
)

type LoginMiddleware struct {
}

var Login = LoginMiddleware{}

// LoginAuth 登录验证
func (l *LoginMiddleware) LoginAuth() gin.HandlerFunc {
	return func(c *gin.Context) {
		//token := c.Query("name")
		//if token == "" {
		//	c.JSON(401, gin.H{
		//		"code": 401,
		//		"msg":  "权限不足",
		//	})
		//	c.Abort()
		//	return
		//}
		//c.Next()

		id, _ := c.Cookie("userId")
		var user models.User
		//models.Db.First(&user,id)
		//查询是否存在
		models.Db.Where("id = ?", id).First(&user)
		if user.Id == 0 {
			c.JSON(401, gin.H{
				"code": 401,
				"msg":  "权限不足",
			})
			c.Abort()
			return
		}
		//if uid == "" {
		//	c.JSON(401, gin.H{
		//		"code": 401,
		//		"msg":  "权限不足",
		//	})
		//	c.Abort()
		//	return
		//}
		c.Next()
	}
}
